package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_UNION_WAR_SET_COMMANDER, desc = "设定指挥官")
public class CrossUnionWarSetCommanderCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long pid = packet.getPlayerId();
        UnionActivityGroupProto.BaseUnionActivityGroupMsg baseMsg = UnionActivityGroupProto.BaseUnionActivityGroupMsg.parseFrom(packet.getBytes());
        packet = YanQuMessageUtils.buildMessageByte(baseMsg.getCode(), baseMsg.getMsg().toByteArray());
        packet.setPlayerId(pid);

        CrossUnionWarProto.CrossUnionWarSetCommanderReqMsg reqMsg = CrossUnionWarProto.CrossUnionWarSetCommanderReqMsg.parseFrom(packet.getBytes());

        ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(reqMsg.getActivityId());
        if(null == activityInfo){
            return;
        }

        UnionActivityGroupUnion unionData = Cross2UnionActivityGroupMgr.getUnionData(reqMsg.getActivityId(), reqMsg.getUserId());
        if(unionData != null){
            CrossUnionWarSimple unionWarSimple = CrossUnionWarMgr.getCrossUnionWarSimple(reqMsg.getActivityId(), unionData.getUnionUid());
            if(unionWarSimple != null){
                unionWarSimple.setCommander(reqMsg.getUserId());
            }
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_SET_COMMANDER,
                CrossUnionWarProto.CrossUnionWarSetCommanderRespMsg.newBuilder().setRet(0)));
    }

    @Override
    public void sendData() {

    }
}
